{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parameter Management\n",
    "\n",
    "Once we have chosen an architecture\n",
    "and set our hyperparameters,\n",
    "we proceed to the training loop,\n",
    "where our goal is to find parameter values\n",
    "that minimize our objective function. \n",
    "After training, we will need these parameters \n",
    "in order to make future predictions.\n",
    "Additionally, we will sometimes wish \n",
    "to extract the parameters \n",
    "either to reuse them in some other context,\n",
    "to save our model to disk so that \n",
    "it may be exectuted in other software,\n",
    "or for examination in the hopes of \n",
    "gaining scientific understanding.\n",
    "\n",
    "Most of the time, we will be able \n",
    "to ignore the nitty-gritty details\n",
    "of how parameters are declared\n",
    "and manipulated, relying on DJL\n",
    "to do the heavy lifting.\n",
    "However, when we move away from \n",
    "stacked architectures with standard layers, \n",
    "we will sometimes need to get into the weeds\n",
    "of declaring and manipulating parameters. \n",
    "In this section, we cover the following:\n",
    "\n",
    "* Accessing parameters for debugging, diagnostics, and visualiziations.\n",
    "* Parameter initialization.\n",
    "* Sharing parameters across different model components.\n",
    "\n",
    "We start by focusing on an MLP with one hidden layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load ../utils/djl-imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NDManager manager = NDManager.newBaseManager();\n",
    "\n",
    "NDArray x = manager.randomUniform(0, 1, new Shape(2, 4));\n",
    "\n",
    "SequentialBlock net = new SequentialBlock();\n",
    "\n",
    "net.add(Linear.builder().setUnits(8).build());\n",
    "net.add(Activation.reluBlock());\n",
    "net.add(Linear.builder().setUnits(1).build());\n",
    "net.setInitializer(new NormalInitializer(), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "\n",
    "ParameterStore ps = new ParameterStore(manager, false);\n",
    "net.forward(ps, new NDList(x), false).head(); // forward computation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameter Access\n",
    "\n",
    "Let us start with how to access parameters\n",
    "from the models that you already know.\n",
    "Each layer's parameters are conveniently stored in a `Pair<String, Parameter>` consisting of a unique\n",
    "`String` that serves as a key for the layer and the `Parameter` itself.\n",
    "The `ParameterList` is an extension of `PairList` and is returned with a call to the `getParameters()` method on a `Block`. \n",
    "We can inspect the parameters of the `net` defined above.\n",
    "When a model is defined via the `SequentialBlock` class,\n",
    "we can access any layer's `Pair<String, Parameter>` by calling `get()` on the `ParameterList` and passing in the index\n",
    "of the parameter we want. Calling `getKey()` and `getValue()` on a `Pair<String, Parameter>` will get the parameter's name and `Parameter` respectively. We can also directly get the `Parameter` we want from the `ParameterList`\n",
    "by calling `get()` and passing in its unique key(the `String` portion of the `Pair<String, Parameter>`. If we call `valueAt()` and pass in\n",
    "the index, we will get the `Parameter` directly as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "2"
    }
   },
   "outputs": [],
   "source": [
    "ParameterList params = net.getParameters();\n",
    "// Print out all the keys (unique!)\n",
    "for (var pair : params) {\n",
    "    System.out.println(pair.getKey());\n",
    "}\n",
    "\n",
    "// Use the unique key to access the Parameter\n",
    "NDArray dense0Weight = params.get(\"01Linear_weight\").getArray();\n",
    "NDArray dense0Bias = params.get(\"01Linear_bias\").getArray();\n",
    "\n",
    "// Use indexing to access the Parameter\n",
    "NDArray dense1Weight = params.valueAt(2).getArray();\n",
    "NDArray dense1Bias = params.valueAt(3).getArray();\n",
    "\n",
    "System.out.println(dense0Weight);\n",
    "System.out.println(dense0Bias);\n",
    "\n",
    "System.out.println(dense1Weight);\n",
    "System.out.println(dense1Bias);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The output tells us a few important things.\n",
    "First, each fully-connected layer \n",
    "has two parameters, e.g., \n",
    "`dense0Weight` and `dense0Bias`,\n",
    "corresponding to that layer's \n",
    "weights and biases, respectively.\n",
    "The `params` variable is a `ParameterList` which contain the\n",
    "key-value pairs of the layer name and a parameter of the \n",
    "`Parameter` class.\n",
    "With a `Parameter`, we can get the underlying numerical values as `NDArray`s by calling \n",
    "`getArray()` on them!\n",
    "Both the weights and biases are stored as single precision floats(`FLOAT32`).\n",
    "\n",
    "\n",
    "### Targeted Parameters\n",
    "\n",
    "Parameters are complex objects,\n",
    "containing data, gradients,\n",
    "and additional information.\n",
    "That's why we need to request the data explicitly.\n",
    "Note that the bias vector consists of zeroes\n",
    "because we have not updated the network\n",
    "since it was initialized."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that unlike the biases, the weights are nonzero. \n",
    "This is because unlike biases, \n",
    "weights are initialized randomly. \n",
    "In addition to `getArray()`, each `Parameter`\n",
    "also provides a `requireGradient()` method which\n",
    "returns whether the parameter needs gradients to be computed\n",
    "(which we set on the `NDArray` with `attachGradient()`).\n",
    "The gradient has the same shape as the weight. \n",
    "To actually access the gradient, we simply call `getGradient()` on the\n",
    "`NDArray`.\n",
    "Because we have not invoked backpropagation \n",
    "for this network yet, its values are all 0.\n",
    "We would invoke it by creating a `GradientCollector` instance and\n",
    "run our calculations inside it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "5"
    }
   },
   "outputs": [],
   "source": [
    "dense0Weight.getGradient();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Collecting Parameters from Nested Blocks\n",
    "\n",
    "Let us see how the parameter naming conventions work \n",
    "if we nest multiple blocks inside each other. \n",
    "For that we first define a function that produces Blocks \n",
    "(a Block factory, so to speak) and then \n",
    "combine these inside yet larger Blocks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "20"
    }
   },
   "outputs": [],
   "source": [
    "public SequentialBlock block1() {\n",
    "    SequentialBlock net = new SequentialBlock();\n",
    "    net.add(Linear.builder().setUnits(32).build());\n",
    "    net.add(Activation.reluBlock());\n",
    "    net.add(Linear.builder().setUnits(16).build());\n",
    "    net.add(Activation.reluBlock());\n",
    "    return net;\n",
    "}\n",
    "\n",
    "public SequentialBlock block2() {\n",
    "    SequentialBlock net = new SequentialBlock();\n",
    "    for (int i = 0; i < 4; i++) {\n",
    "        net.add(block1());\n",
    "    }\n",
    "    return net;\n",
    "}\n",
    "\n",
    "SequentialBlock rgnet = new SequentialBlock();\n",
    "rgnet.add(block2());\n",
    "rgnet.add(Linear.builder().setUnits(10).build());\n",
    "rgnet.setInitializer(new NormalInitializer(), Parameter.Type.WEIGHT);\n",
    "rgnet.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "\n",
    "rgnet.forward(ps, new NDList(x), false).singletonOrThrow();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have designed the network, \n",
    "let us see how it is organized.\n",
    "We can get the list of named parameters by calling `getParameters()`.\n",
    "However, we not only want to see the parameters, but also how\n",
    "our network is structured.\n",
    "To see our network architecture, we can simply print out the block whose architecture we want to see."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/* Network Architecture for RgNet */\n",
    "rgnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/* Parameters for RgNet */\n",
    "for (var param : rgnet.getParameters()) {\n",
    "    System.out.println(param.getValue().getArray());\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since the layers are hierarchically nested,\n",
    "we can also access them by calling their `getChildren()` method\n",
    "to get a `BlockList`(also an extension of `PairList`) of their inner blocks.\n",
    "It shares methods with `ParameterList` and as such we can use their\n",
    "familiar structure to access the blocks. We can call `get(i)` to get the\n",
    "`Pair<String, Block>` at the index `i` we want, and then finally `getValue()` to get the actual\n",
    "block. We can do this in one step as shown above with `valueAt(i)`. Then we have to repeat that to get that blocks child and so on.\n",
    "\n",
    "Here, we access the first major block, \n",
    "within it the second subblock, \n",
    "and within that the bias of the first layer,\n",
    "with as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Block majorBlock1 = rgnet.getChildren().get(0).getValue();\n",
    "Block subBlock2 = majorBlock1.getChildren().valueAt(1);\n",
    "Block linearLayer1 = subBlock2.getChildren().valueAt(0);\n",
    "NDArray bias = linearLayer1.getParameters().valueAt(1).getArray();\n",
    "bias"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameter Initialization\n",
    "\n",
    "Now that we know how to access the parameters,\n",
    "let us look at how to initialize them properly.\n",
    "We discussed the need for initialization in :numref:`sec_numerical_stability`. \n",
    "By default, DJL initializes weight matrices\n",
    "based on your set initializer \n",
    "and the bias parameters are all set to $0$.\n",
    "However, we will often want to initialize our weights\n",
    "according to various other protocols. \n",
    "DJL's `ai.djl.training.initializer` package provides a variety \n",
    "of preset initialization methods.\n",
    "If we want to create a custom initializer,\n",
    "we need to do some extra work.\n",
    "\n",
    "### Built-in Initialization\n",
    "\n",
    "In DJL, when setting the initializer for blocks, the default `setInitializer()` function does not overwrite\n",
    "any previous set initializers. So if you set an initializer earlier, but decide you want to change your initializer and call `setInitializer()` again, the second `setInitializer()` will NOT overwrite your first one.\n",
    "\n",
    "Additionally, when you call `setInitializer()` on a block, all internal blocks will also call `setInitializer()` with the same given `initializer`.\n",
    "\n",
    "This means that we can call `setInitializer()` on the highest level of a block and know that all internal blocks that do not have an initializer already set will be set to that given `initializer`.\n",
    "\n",
    "This setup has the advantage that we don't have to worry about our `setInitializer()` overriding our previous `initializer`s on internal blocks!\n",
    "\n",
    "If you want to however, you can explicitly set an initializer for a `Parameter` by calling its `setInitializer()` function directly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us begin by calling on built-in initializers. The code below initializes all parameters \n",
    "to a given constant value 1, by using the `ConstantInitializer()` initializer. \n",
    "\n",
    "Note that this will not do anything currently since we have already set\n",
    "our initializer in the previous code block.\n",
    "We can verify this by checking the weight of a parameter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.setInitializer(new ConstantInitializer(1), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "Block linearLayer = net.getChildren().get(0).getValue();\n",
    "NDArray weight = linearLayer.getParameters().get(0).getValue().getArray();\n",
    "weight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see these initializations however if we create a new network.\n",
    "Let us write a function to create these network architectures for us\n",
    "conveniently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public SequentialBlock getNet() {\n",
    "    SequentialBlock net = new SequentialBlock();\n",
    "    net.add(Linear.builder().setUnits(8).build());\n",
    "    net.add(Activation.reluBlock());\n",
    "    net.add(Linear.builder().setUnits(1).build());\n",
    "    return net;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we run our previous initializer on this new net and check a parameter, we'll\n",
    "see that everything is initialized properly! (to 7777!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SequentialBlock net = getNet();\n",
    "net.setInitializer(new ConstantInitializer(7777), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "Block linearLayer = net.getChildren().valueAt(0);\n",
    "NDArray weight = linearLayer.getParameters().valueAt(0).getArray();\n",
    "weight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also initialize all parameters \n",
    "as Gaussian random variables \n",
    "with standard deviation $.01$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "10"
    }
   },
   "outputs": [],
   "source": [
    "net = getNet();\n",
    "net.setInitializer(new NormalInitializer(), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "Block linearLayer = net.getChildren().valueAt(0);\n",
    "NDArray weight = linearLayer.getParameters().valueAt(0).getArray();\n",
    "weight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also apply different initializers for certain Blocks.\n",
    "For example, below we initialize the first layer\n",
    "with the `XavierInitializer` initializer\n",
    "and initialize the second layer \n",
    "to a constant value of 0.\n",
    "\n",
    "We will do this without the `getNet()` function as it will be easier\n",
    "to have the reference to each block we want to set.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "11"
    }
   },
   "outputs": [],
   "source": [
    "net = new SequentialBlock();\n",
    "Linear linear1 = Linear.builder().setUnits(8).build();\n",
    "net.add(linear1);\n",
    "net.add(Activation.reluBlock());\n",
    "Linear linear2 = Linear.builder().setUnits(1).build();\n",
    "net.add(linear2);\n",
    "\n",
    "linear1.setInitializer(new XavierInitializer(), Parameter.Type.WEIGHT);\n",
    "linear1.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "\n",
    "linear2.setInitializer(Initializer.ZEROS, Parameter.Type.WEIGHT);\n",
    "linear2.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "\n",
    "System.out.println(linear1.getParameters().valueAt(0).getArray());\n",
    "System.out.println(linear2.getParameters().valueAt(0).getArray());"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can directly access the `Parameter.setInitializer()` and set their initializers individually."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = getNet();\n",
    "ParameterList params = net.getParameters();\n",
    "\n",
    "params.get(\"01Linear_weight\").setInitializer(new NormalInitializer());\n",
    "params.get(\"03Linear_weight\").setInitializer(Initializer.ONES);\n",
    "\n",
    "net.initialize(manager, DataType.FLOAT32, new Shape(2, 4));\n",
    "\n",
    "System.out.println(params.valueAt(0).getArray());\n",
    "System.out.println(params.valueAt(2).getArray());"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom Initialization\n",
    "\n",
    "Sometimes, the initialization methods we need \n",
    "are not standard in DJL. \n",
    "In these cases, we can define a class to implement the `Initializer` interface. \n",
    "We only have to implement the `initialize()` function,\n",
    "which takes an `NDManager`, a `Shape`, and the `DataType`. \n",
    "We then create the `NDArray` with the aforementioned `Shape` and `DataType`\n",
    "and initialize it to what we want! You can also design your\n",
    "initializer to take in some parameters. Simply declare them\n",
    "as fields in the class and pass them in as inputs to the constructor!\n",
    "In the example below, we define an initializer\n",
    "for the following strange distribution:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "    w \\sim \\begin{cases}\n",
    "        U[5, 10] & \\text{ with probability } \\frac{1}{4} \\\\\n",
    "            0    & \\text{ with probability } \\frac{1}{2} \\\\\n",
    "        U[-10, -5] & \\text{ with probability } \\frac{1}{4}\n",
    "    \\end{cases}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "static class MyInit implements Initializer {\n",
    "\n",
    "    public MyInit() {}\n",
    "\n",
    "    @Override\n",
    "    public NDArray initialize(NDManager manager, Shape shape, DataType dataType) {\n",
    "        System.out.printf(\"Init %s\\n\", shape.toString());\n",
    "        // Here we generate data points \n",
    "        // from a uniform distribution [-10, 10]\n",
    "        NDArray data = manager.randomUniform(-10, 10, shape, dataType);\n",
    "        // We keep the data points whose absolute value is >= 5\n",
    "        // and set the others to 0.\n",
    "        // This generates the distribution `w` shown above.\n",
    "        NDArray absGte5 = data.abs().gte(5); // returns boolean NDArray where \n",
    "                                             // true indicates abs >= 5 and\n",
    "                                             // false otherwise\n",
    "        return data.mul(absGte5); // keeps true indices and sets false indices to 0.\n",
    "                                  // special operation when multiplying a numerical\n",
    "                                  // NDArray with a boolean NDArray\n",
    "    }\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = getNet();\n",
    "net.setInitializer(new MyInit(), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "Block linearLayer = net.getChildren().valueAt(0);\n",
    "NDArray weight = linearLayer.getParameters().valueAt(0).getArray();\n",
    "weight"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that we always have the option \n",
    "of setting parameters directly by calling `getValue().getArray()` \n",
    "to access the underlying `NDArray`. \n",
    "A note for advanced users: \n",
    "you cannot directly modify parameters within a `GarbageCollector` scope.\n",
    "You must modify them outside the `GarbageCollector` scope to avoid confusing \n",
    "the automatic differentiation mechanics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "13"
    }
   },
   "outputs": [],
   "source": [
    "// '__'i() is an inplace operation to modify the original NDArray\n",
    "NDArray weightLayer = net.getChildren().valueAt(0)\n",
    "    .getParameters().valueAt(0).getArray();\n",
    "weightLayer.addi(7);\n",
    "weightLayer.divi(9);\n",
    "weightLayer.set(new NDIndex(0, 0), 2020); // set the (0, 0) index to 2020\n",
    "weightLayer;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tied Parameters\n",
    "\n",
    "Often, we want to share parameters across multiple layers.\n",
    "Later we will see that when learning word embeddings,\n",
    "it might be sensible to use the same parameters\n",
    "both for encoding and decoding words. \n",
    "We discussed one such case when we introduced :numref:`sec_model_construction`. \n",
    "Let us see how to do this a bit more elegantly. \n",
    "In the following we allocate a dense layer \n",
    "and then use its parameters specifically \n",
    "to set those of another layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SequentialBlock net = new SequentialBlock();\n",
    "\n",
    "// We need to give the shared layer a name \n",
    "// such that we can reference its parameters\n",
    "Block shared = Linear.builder().setUnits(8).build();\n",
    "SequentialBlock sharedRelu = new SequentialBlock();\n",
    "sharedRelu.add(shared);\n",
    "sharedRelu.add(Activation.reluBlock());\n",
    "\n",
    "net.add(Linear.builder().setUnits(8).build());\n",
    "net.add(Activation.reluBlock());\n",
    "net.add(sharedRelu);\n",
    "net.add(sharedRelu);\n",
    "net.add(Linear.builder().setUnits(10).build());\n",
    "\n",
    "NDArray x = manager.randomUniform(-10f, 10f, new Shape(2, 20), DataType.FLOAT32);\n",
    "\n",
    "net.setInitializer(new NormalInitializer(), Parameter.Type.WEIGHT);\n",
    "net.initialize(manager, DataType.FLOAT32, x.getShape());\n",
    "\n",
    "net.forward(ps, new NDList(x), false).singletonOrThrow();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Check that the parameters are the same\n",
    "NDArray shared1 = net.getChildren().valueAt(2)\n",
    "    .getParameters().valueAt(0).getArray();\n",
    "NDArray shared2 = net.getChildren().valueAt(3)\n",
    "    .getParameters().valueAt(0).getArray();\n",
    "shared1.eq(shared2);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows that the parameters \n",
    "of the second and third layer are tied. \n",
    "They are not just equal, they are \n",
    "represented by the same exact `NDArray`. \n",
    "Thus, if we change one of the parameters,\n",
    "the other one changes, too. \n",
    "You might wonder, \n",
    "*when parameters are tied\n",
    "what happens to the gradients?*\n",
    "Since the model parameters contain gradients,\n",
    "the gradients of the second hidden layer\n",
    "and the third hidden layer are added together\n",
    "in `shared.getGradient()` during backpropagation.\n",
    "\n",
    "## Summary\n",
    "\n",
    "* We have several ways to access, initialize, and tie model parameters.\n",
    "* We can use custom initialization.\n",
    "* DJL has a sophisticated mechanism for accessing parameters in a unique and hierarchical manner.\n",
    "\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. Use the FancyMLP defined in :numref:`sec_model_construction` and access the parameters of the various layers.\n",
    "1. Look at the [DJL documentation](https://javadoc.io/doc/ai.djl/api/latest/ai/djl/training/initializer/Initializer.html) and explore different initializers.\n",
    "1. Try accessing the model parameters after `net.initialize()` and before `predictor.predict(x)` to observe the shape of the model parameters. What changes? Why?\n",
    "1. Construct a multilayer perceptron containing a shared parameter layer and train it. During the training process, observe the model parameters and gradients of each layer.\n",
    "1. Why is sharing parameters a good idea?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "14.0.2+12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
